package com.foreknow.web;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.foreknow.entity.Scheduling;
import com.foreknow.entity.User;
import com.foreknow.service.SchedulingService;
import com.foreknow.utils.Constance;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/scheduling")
public class SchedulingController {
    @Autowired
    private SchedulingService schedulingService;
    @RequestMapping(value = "/findSchedulingByPage", method = RequestMethod.POST)
    public String findUserByPage(@RequestBody Map<String, String> para) throws JsonProcessingException {
        Integer pageNum = Integer.parseInt(para.get("pageNum"));
        Integer pageSize = Integer.parseInt(para.get("pageSize"));
        String startTime = para.get("startTime");
        String endTime = para.get("endTime");
        HashMap<String, Object> hs = new HashMap<>();
        //设置分页信息
        PageHelper.startPage(pageNum, pageSize);
        List<Scheduling> schedulingList = schedulingService.querySchedulingByTime(startTime,endTime);
        PageInfo<Scheduling> info = new PageInfo<>(schedulingList);
        //给map添加数据
        hs.put("totalpage", info.getTotal());
        hs.put("pagenum", info.getPageNum());
        hs.put("Schedulings", schedulingList);
        if (schedulingList.size() > 0) {//如果获取到数据
            hs.put("meta", new Constance("获取成功", 200));
        } else {
            hs.put("meta", new Constance("没有查询到数据", 401));
        }
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }

    @RequestMapping(value = "/addScheduling", method = RequestMethod.GET)
    public String findUserByPage() throws JsonProcessingException {
        HashMap<String, Object> hs = new HashMap<>();
        if(schedulingService.addScheduling()>0){
            hs.put("meta", new Constance("添加成功", 200));
        } else {
            hs.put("meta", new Constance("添加失败", 401));
        }
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }
}
